<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/4/13
 *  Q Q: 505413
 * Time: 15:35
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\MemberGroupModel;
use app\admin\model\MemberModel;
use think\Db;
class Member extends Base
{
    //会员组展示
    public function group()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['group_name'] = ['like',"%" . $key . "%"];
        }
        $lstObj = Db::name('member_group')->where($map)->select();
        $this->assign('lstObj',$lstObj);
        return $this->fetch();
    }
    //修改列表的开启状态
    public function status_edit()
    {
        $id = input('param.id');
        $status = Db::name('member_group')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('member_group')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('member_group')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加会员组
    public function groupAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['create_time'] = time();
            $param['update_time'] = time();
            $res = Db::name('member_group')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'member/group', 'msg' => '添加会员组成功']);
            }else{
                return json(['code' => -1, 'url' => 'member/group', 'msg' => '添加会员组失败，请重试']);
            }
        }
        return $this->fetch();
    }
    //编辑会员组
    public function groupEdit()
    {
        $group = new MemberGroupModel();
        if(request()->isPost()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $flag = $group->editGroup($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('group',$group->getOne($id));
        return $this->fetch();
    }
    //删除会员组
    public function groupDel()
    {
        $id = input('id');
        $res = Db::name('member_group')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除会员组成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除会员组失败，请重试']);
        }
    }

    //*********************************************会员列表*********************************************//
    public function index()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['account'] = ['like',"%" . $key . "%"];
        }
        $lst = Db::name('member')
            ->alias('a')
            ->field('a.id,account,nickname,sex,integral,mobile,group_name,a.create_time,b.update_time,login_num,a.status')
            ->join('member_group b ','a.group_id= b.id')
            ->where($map)
            ->paginate(10);
        $this->assign('lst',$lst);
        return $this->fetch();
    }
    //修改列表的开启状态
    public function status_edit_m()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }
    //添加会员
    public function memberAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            $param['password'] = md5($param['password']);
            $param['create_time'] = time();
            $param['update_time'] = time();
            $member = new MemberModel();
            //dump($param);exit;
            $res = $member->insertMember($param);
            if(false === $res){
                return json(['code' => -1, 'url' => 'member/index', 'msg' => '添加会员失败']);
            }else{
                return json(['code' => 1, 'url' => 'member/index', 'msg' => '添加会员成功']);
            }
        }
        $group = new MemberGroupModel();
        $this->assign('group',$group->getGroup());
        return $this->fetch();
    }
    //编辑会员
    public function memberEdit()
    {
        $member = new MemberModel();
        $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['status'])){
                if($param['status'] == 'on') {
                    $param['status'] = '1';
                }
            }else{
                $param['status'] = '0';
            }
            if(empty($param['password'])){
                unset($param['password']);
            }else{
                $param['password'] = md5($param['password']);
            }
            $param['update_time'] = time();
            $flag = $member->editMember($param);
            if($flag){
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }else{
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }
        }

        $group = new MemberGroupModel();
        $this->assign([
            'member' => $member->getOneMember($id),
            'group' => $group->getGroup()
        ]);
        return $this->fetch();
    }
    //删除会员
    public function memberDel()
    {
        $id = input('id');
        $res = Db::name('member')->where(array('id'=>$id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除用户成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除用户，请重试']);
        }
    }
}